package jp.oesf.databasesample;

import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

public class DatabaseSample extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

	}
	
	public void onClickSearchButton(View v){
		Intent intent = new Intent(this, ResultListActivity.class);
		startActivity(intent);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
        MenuInflater menuInfalter = getMenuInflater();
        menuInfalter.inflate(R.menu.menu, menu);
        return true;

	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		Intent intent = new Intent(this, AddActivity.class);
		startActivity(intent);
		return true;
	}
	
	
	public void onClickDrop(View v){
		String droptable = "DROP TABLE if exists " + SampleSQLiteOpenHelper.SAMPLE_TABLE + ";";
		SampleSQLiteOpenHelper databaseOpenHelper = new SampleSQLiteOpenHelper(this);
		SQLiteDatabase database = null;
		
		// 読込専用のSQLiteDatabaseオブジェクトを取得する
		database = databaseOpenHelper.getWritableDatabase();
		database.execSQL(droptable);
		
		database.execSQL("CREATE TABLE SAMPLE_TABLE(_id INTEGER PRIMARY KEY AUTOINCREMENT" +
				",name TEXT not null" +
				",value INTEGER not null" +
				");");

		Toast.makeText(this, "sample table を削除", Toast.LENGTH_SHORT).show();
		databaseOpenHelper.close();

	}
	
}